


cd "/Users/sergi/Library/CloudStorage/Dropbox/Work/Coconut paper/APS data/"

global path "/Users/sergi/Library/CloudStorage/Dropbox/Work/Coconut paper/ESS data/"

global pathfig "/Users/sergi/Library/CloudStorage/Dropbox/Aplicaciones/Overleaf/Migrant political integration/"

global results "/Users/sergi/Library/CloudStorage/Dropbox/Work/Coconut paper/APS data/"


set scheme gg_tableau

*2010

use "aps_2010.dta", clear

gen year_arrived = cameyr /*year arrived*/
replace year_arrived=. if year_arrived <1948

recode cry01 -9/-8=. /*create foreign born variable to indicate born outside UK and Ireland and non-white*/
recode cry01 997=1 921/926=0 else=., gen(foreign) 
replace foreign =. if ethcen6==-9
replace foreign =. if ethcen6==-8
replace foreign =. if foreign==1 & ethcen6==1


gen yearbirth = 2010 - age /*match year of arrival to year when UK-born turned 17*/
gen year17 = yearbirth+17
replace year_arrived = year17 if foreign==0
replace year_arrived =. if year_arrived < 1948
replace year_arrived =. if year_arrived > 2010


gen age_arrival = year_arrived - yearbirth


gen unemployed=ilodefr==2 /*unemployed*/
replace durun=. if durun<0 /*duration of unemployment*/

recode inecac05 16=1 24=1 else=0, gen(retired) /*retired*/

replace net99=. if net99<0 /*net pay*/
xtile net99_dec = net99, nq(10) /*net pay in deciles*/

replace hourpay=. if hourpay<0 /*gross hourly pay*/
xtile hourpay_dec=hourpay, nq(10)

replace grsswk=. if grsswk<0 /*gross monthly pay*/
xtile grsswk_dec=grsswk, nq(10)


recode jobtyp 1=1 2=0 else=., gen(permanent) /*permanent job yes or no*/

recode sex 2=1 1=0, gen(female)


recode edageband -9/-8=. 95=0, gen(educ_years)

gen wave=2010

keep year_arrived foreign ethcen6 yearbirth year17 age_arrival unemployed durun retired ///
net99 net99_dec hourpay hourpay_dec grsswk grsswk_dec permanent age female educ_years wave

save  "aps_2010_short.dta", replace



*2009

use "aps_2009.dta", clear

gen year_arrived = cameyr /*year arrived*/
replace year_arrived=. if year_arrived <1948


recode cry01 -9/-8=. /*create foreign born variable to indicate born outside UK and Ireland and non-white*/
recode cry01 997=1 921/926=0 else=., gen(foreign) 
replace foreign =. if ethcen6==-9
replace foreign =. if ethcen6==-8
replace foreign =. if foreign==1 & ethcen6==1



gen yearbirth = 2009 - age /*match year of arrival to year when UK-born turned 17*/
gen year17 = yearbirth+17
replace year_arrived = year17 if foreign==0
replace year_arrived =. if year_arrived < 1948
replace year_arrived =. if year_arrived > 2009

gen age_arrival = year_arrived - yearbirth


gen unemployed=ilodefr==2 /*unemployed*/
replace durun=. if durun<0 /*duration of unemployment*/

recode inecac05 16=1 24=1 else=0, gen(retired) /*retired*/


replace net99=. if net99<0 /*net pay*/
xtile net99_dec = net99, nq(10) /*net pay in deciles*/

replace hourpay=. if hourpay<0 /*gross hourly pay*/
xtile hourpay_dec=hourpay, nq(10)

replace grsswk=. if grsswk<0 /*gross monthly pay*/
xtile grsswk_dec=grsswk, nq(10)



recode jobtyp 1=1 2=0 else=., gen(permanent) /*permanent job yes or no*/

recode sex 2=1 1=0, gen(female)

recode edageband -9/-8=. 95=0, gen(educ_years)

gen wave=2009 

keep year_arrived foreign ethcen6 yearbirth year17 age_arrival unemployed durun retired ///
net99 net99_dec hourpay hourpay_dec grsswk grsswk_dec permanent age female educ_years wave

save  "aps_2009_short.dta", replace



*2008

use "aps_2008.dta", clear

gen year_arrived = CAMEYR /*year arrived*/
replace year_arrived=. if year_arrived <1948


recode CRY01 -9/-8=. /*create foreign born variable to indicate born outside UK and Ireland and non-white*/
recode CRY01 997=1 921/926=0 else=., gen(foreign) 
replace foreign =. if ETHCEN6==-9
replace foreign =. if ETHCEN6==-8
replace foreign =. if foreign==1 & ETHCEN6==1



gen yearbirth = 2008 - AGE /*match year of arrival to year when UK-born turned 17*/
gen year17 = yearbirth+17
replace year_arrived = year17 if foreign==0
replace year_arrived =. if year_arrived < 1948
replace year_arrived =. if year_arrived > 2008

gen age_arrival = year_arrived - yearbirth

gen age=AGE


gen unemployed=ILODEFR==2 /*unemployed*/
replace DURUN=. if DURUN<0 /*duration of unemployment*/

recode INECAC05 16=1 24=1 else=0, gen(retired) /*retired*/


replace NET99=. if NET99<0 /*net pay*/
gen net99=NET99
xtile net99_dec = NET99, nq(10) /*net pay in deciles*/

replace HOURPAY=. if HOURPAY<0 /*gross hourly pay*/
gen hourpay=HOURPAY
xtile hourpay_dec=HOURPAY, nq(10)

replace GRSSWK=. if GRSSWK<0 /*gross monthly pay*/
gen grsswk=GRSSWK
xtile grsswk_dec=GRSSWK, nq(10)



recode JOBTYP 1=1 2=0 else=., gen(permanent) /*permanent job yes or no*/

recode SEX 2=1 1=0, gen(female)

recode edageband -9/-8=. 95=0, gen(educ_years)

gen wave=2008

gen ethcen6=ETHCEN6
gen durun=DURUN

keep year_arrived foreign ethcen6 yearbirth year17 age_arrival unemployed durun retired ///
net99 net99_dec hourpay hourpay_dec grsswk grsswk_dec permanent age female educ_years wave

save  "aps_2008_short.dta", replace



*2007

use "aps_2007.dta", clear

gen year_arrived = CAMEYR /*year arrived*/
replace year_arrived=. if year_arrived <1948


recode CRY01 -9/-8=. /*create foreign born variable to indicate born outside UK and Ireland and non-white*/
recode CRY01 997=1 921/926=0 else=., gen(foreign) 
replace foreign =. if ETHCEN6==-9
replace foreign =. if ETHCEN6==-8
replace foreign =. if foreign==1 & ETHCEN6==1



gen yearbirth = 2007 - AGE /*match year of arrival to year when UK-born turned 17*/
gen year17 = yearbirth+17
replace year_arrived = year17 if foreign==0
replace year_arrived =. if year_arrived < 1948
replace year_arrived =. if year_arrived > 2007

gen age_arrival = year_arrived - yearbirth

gen age=AGE



gen unemployed=ILODEFR==2 /*unemployed*/
replace DURUN=. if DURUN<0 /*duration of unemployment*/

recode INECAC05 16=1 24=1 else=0, gen(retired) /*retired*/



replace NET99=. if NET99<0 /*net pay*/
gen net99=NET99
xtile net99_dec = NET99, nq(10) /*net pay in deciles*/

replace HOURPAY=. if HOURPAY<0 /*gross hourly pay*/
gen hourpay=HOURPAY
xtile hourpay_dec=HOURPAY, nq(10)

replace GRSSWK=. if GRSSWK<0 /*gross monthly pay*/
gen grsswk=GRSSWK
xtile grsswk_dec=GRSSWK, nq(10)



recode JOBTYP 1=1 2=0 else=., gen(permanent) /*permanent job yes or no*/

recode SEX 2=1 1=0, gen(female)

recode edageband -9/-8=. 95=0, gen(educ_years)

gen wave=2007

gen ethcen6=ETHCEN6
gen durun=DURUN

keep year_arrived foreign ethcen6 yearbirth year17 age_arrival unemployed durun retired ///
net99 net99_dec hourpay hourpay_dec grsswk grsswk_dec permanent age female educ_years wave

save  "aps_2007_short.dta", replace




*2006

use "aps_2006.dta", clear

gen year_arrived = CAMEYR /*year arrived*/
replace year_arrived=. if year_arrived <1948


recode CRY01 -9/-8=. /*create foreign born variable to indicate born outside UK and Ireland and non-white; we include Hong Kong as it was Commonwealth but exclude China*/
recode CRY01 59=1 1/5=0 36=1 else=., gen(foreign) 
replace foreign =. if ETHCEN6==-9
replace foreign =. if ETHCEN6==-8
replace foreign =. if foreign==1 & ETHCEN6==1



gen yearbirth = 2006 - AGE /*match year of arrival to year when UK-born turned 17*/
gen year17 = yearbirth+17
replace year_arrived = year17 if foreign==0
replace year_arrived =. if year_arrived < 1948
replace year_arrived =. if year_arrived > 2006

gen age_arrival = year_arrived - yearbirth

gen age=AGE



gen unemployed=ILODEFR==2 /*unemployed*/
replace DURUN=. if DURUN<0 /*duration of unemployment*/

recode INECAC05 16=1 24=1 else=0, gen(retired) /*retired*/



replace NET99=. if NET99<0 /*net pay*/
gen net99=NET99
xtile net99_dec = NET99, nq(10) /*net pay in deciles*/

replace HOURPAY=. if HOURPAY<0 /*gross hourly pay*/
gen hourpay=HOURPAY
xtile hourpay_dec=HOURPAY, nq(10)

replace GRSSWK=. if GRSSWK<0 /*gross monthly pay*/
gen grsswk=GRSSWK
xtile grsswk_dec=grsswk, nq(10)



recode JOBTYP 1=1 2=0 else=., gen(permanent) /*permanent job yes or no*/

recode SEX 2=1 1=0, gen(female)

recode edageband -9/-8=. 95=0, gen(educ_years)

gen wave=2006

gen ethcen6=ETHCEN6
gen durun=DURUN

keep year_arrived foreign ethcen6 yearbirth year17 age_arrival unemployed durun retired ///
net99 net99_dec hourpay hourpay_dec grsswk grsswk_dec permanent age female educ_years wave

save  "aps_2006_short.dta", replace



*2005

use "aps_2005.dta", clear


gen year_arrived = cameyr /*year arrived*/
replace year_arrived=. if year_arrived <1948


recode cry01 -9/-8=. /*create foreign born variable to indicate born outside UK and Ireland and non-white; we include Hong Kong as it was Commonwealth but exclude China*/
recode cry01 59=1 1/5=0 36=1 else=., gen(foreign) 
replace foreign =. if ethcen6==-9
replace foreign =. if ethcen6==-8
replace foreign =. if foreign==1 & ethcen6==1



gen yearbirth = 2005 - age /*match year of arrival to year when UK-born turned 17*/
gen year17 = yearbirth+17
replace year_arrived = year17 if foreign==0
replace year_arrived =. if year_arrived < 1948
replace year_arrived =. if year_arrived > 2005

gen age_arrival = year_arrived - yearbirth


gen unemployed=ilodefr==2 /*unemployed*/
replace durun=. if durun<0 /*duration of unemployment*/

recode inecac05 16=1 24=1 else=0, gen(retired) /*retired*/



replace net99=. if net99<0 /*net pay*/
xtile net99_dec = net99, nq(10) /*net pay in deciles*/

replace hourpay=. if hourpay<0 /*gross hourly pay*/
xtile hourpay_dec=hourpay, nq(10)

replace grsswk=. if grsswk<0 /*gross monthly pay*/
xtile grsswk_dec=grsswk, nq(10)



recode jobtyp 1=1 2=0 else=., gen(permanent) /*permanent job yes or no*/

recode sex 2=1 1=0, gen(female)

recode edageband -9/-8=. 95=0, gen(educ_years)

gen wave=2005


keep year_arrived foreign ethcen6 yearbirth year17 age_arrival unemployed durun retired ///
net99 net99_dec hourpay hourpay_dec grsswk grsswk_dec permanent age female educ_years wave

save  "aps_2005_short.dta", replace


*2004

use "aps_2004.dta", clear


gen year_arrived = cameyr /*year arrived*/
replace year_arrived=. if year_arrived <1948


recode cry01 -9/-8=. /*create foreign born variable to indicate born outside UK and Ireland and non-white*/
recode cry01 59=1 1/5=0 else=., gen(foreign) 
replace foreign =. if ethcen6==-9
replace foreign =. if ethcen6==-8
replace foreign =. if foreign==1 & ethcen6==1



gen yearbirth = 2004 - age /*match year of arrival to year when UK-born turned 17*/
gen year17 = yearbirth+17
replace year_arrived = year17 if foreign==0
replace year_arrived =. if year_arrived < 1948
replace year_arrived =. if year_arrived > 2004

gen age_arrival = year_arrived - yearbirth



gen unemployed=ilodefr==2 /*unemployed*/
replace durun=. if durun<0 /*duration of unemployment*/

recode redylft 7=1 else=0, gen(retired) /*retired: inecacr variable does not seem to code retired respondents. I use redylft instead but might be less accurate*/



replace net99=. if net99<0 /*net pay*/
xtile net99_dec = net99, nq(10) /*net pay in deciles*/

replace hourpay=. if hourpay<0 /*gross hourly pay*/
xtile hourpay_dec=hourpay, nq(10)

replace grsswk=. if grsswk<0 /*gross monthly pay*/
xtile grsswk_dec=grsswk, nq(10)



recode jobtyp 1=1 2=0 else=., gen(permanent) /*permanent job yes or no*/

recode sex 2=1 1=0, gen(female)

recode edageband -9/-8=. 95=0, gen(educ_years)

gen wave=2004

keep year_arrived foreign ethcen6 yearbirth year17 age_arrival unemployed durun retired ///
net99 net99_dec hourpay hourpay_dec grsswk grsswk_dec permanent age female educ_years wave

save  "aps_2004_short.dta", replace




**********
*ANALYSES*
**********

use "aps_2004_short.dta", clear
append using "aps_2005_short.dta"
append using "aps_2006_short.dta"
append using "aps_2007_short.dta"
append using "aps_2008_short.dta"
append using "aps_2009_short.dta"
append using "aps_2010_short.dta"

gen treated=foreign

sort year_arrived

*capture drop  treated 
*gen treated=.
*replace treated=1 if foreign==1
*replace treated=0 if foreign==0

foreach i of num  1962 1973 1983{
capture drop  intervention_`i' time_interv_`i' treated_time_`i' treated_interv_`i' treated_interv_time_`i'
gen time_elapsed_`i'=year_arrived-`i'
gen intervention_`i'=.
replace intervention_`i'=0 if year_arrived<`i'
replace intervention_`i'=1 if year_arrived>=`i'
gen time_interv_`i'=time_elapsed_`i'*intervention_`i'
gen treated_time_`i'=treated*time_elapsed_`i'
gen treated_interv_`i'=treated*intervention_`i'
gen treated_interv_time_`i'=treated*intervention_`i'*time_elapsed_`i'
}




*Table 3 - Difference in differences models predicting economic integration


xtset year_arrived

xtreg grsswk treated intervention_1962 treated_interv_1962 age female educ_years i.wave if age_arrival>15 & age>=59 & retired ==0 & year_arrived<1973, fe vce(cluster year_arrived) 
outreg2  using "${results}aps_diff_models.xls",  bdec(3) sdec(3) append

xtreg unemployed treated intervention_1962 treated_interv_1962 age female educ_years i.wave  if age_arrival>15 & age>=59 & retired ==0 & year_arrived<1973, fe vce(cluster year_arrived) 
outreg2  using "${results}aps_diff_models.xls",  bdec(3) sdec(3) append

xtreg permanent treated intervention_1962 treated_interv_1962 age female educ_years i.wave if age_arrival>15 & age>=59 & retired ==0 & year_arrived<1973, fe vce(cluster year_arrived) 
outreg2  using "${results}aps_diff_models.xls",  bdec(3) sdec(3) append



*Table 4- Interrupted time series analyses

reg grsswk time_elapsed_1962 intervention_1962 time_interv_1962 treated treated_time_1962 treated_interv_1962 treated_interv_time_1962 ///
age female educ_years i.wave if age_arrival>15 & age>=59 & retired ==0, vce(cluster year_arrived) /*yes positive break*/

est store control1

outreg2  using "${results}aps_its_1962.xls",  bdec(3) sdec(3) append


reg unemployed time_elapsed_1962 intervention_1962 time_interv_1962 treated treated_time_1962 treated_interv_1962 treated_interv_time_1962 ///
age female educ_years i.wave if age_arrival>15 & age>=59 & retired ==0, vce(cluster year_arrived) 

est store control2
outreg2  using "${results}aps_its_1962.xls",  bdec(3) sdec(3) append


reg permanent time_elapsed_1962 intervention_1962 time_interv_1962 treated treated_time_1962 treated_interv_1962 treated_interv_time_1962 ///
age female educ_years i.wave if age_arrival>15 & age>=59 & retired ==0, vce(cluster year_arrived) /*positive slope*/

est store control3

outreg2  using "${results}aps_its_1962.xls",  bdec(3) sdec(3) append


*Appendix A - Table A3: Descriptive stats

sum grsswk unemployed permanent treated intervention_1962 age female educ_years

